package com.self.smallma.myblogweb.web;


import com.self.smallma.myblogweb.common.utils.PageUtils;

import com.self.smallma.myblogweb.entity.UserEntity;
import com.self.smallma.myblogweb.service.BlogService;
import com.self.smallma.myblogweb.vo.FirstPageBlogVo;
import com.self.smallma.myblogweb.vo.RecommendBlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    BlogService blogService;

    @GetMapping({"/","index.html"})
    public String index(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                        HttpSession session){

        Map<String, Object> map = new HashMap<>();
        map.put("page",pageNum.toString());
        map.put("limit","5");
        PageUtils pageInfo =  blogService.getAllFirstPageBlog(map);
        pageInfo.getList().forEach(System.out::println);

        //查找是否开启推荐的文章
        List<RecommendBlogVo> recommendedBlog =  blogService.getRecommendedBlog();
        model.addAttribute("recommendedBlogs", recommendedBlog);
        model.addAttribute("pageInfo",pageInfo);




        return "index";
    }


    @PostMapping("/search")
    public String search(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                         @RequestParam String key, Model model){
        Map<String, Object> params = new HashMap<>();
        System.out.println(key);
//        params.put("key",key);
        params.put("limit","5");
        params.put("page",pageNum.toString());
        PageUtils searchBlog = blogService.getSearchBlog(params,key);
        System.out.println(searchBlog);
        model.addAttribute("searchBlog", searchBlog);

        searchBlog.getList().forEach(System.out::println);


        return "search";
    }

    //    博客信息
    @GetMapping("/footer/blogmessage")
    public String blogMessage(Model model){
        int blogTotal = blogService.getBlogTotal();
        int blogViewTotal = blogService.getBlogViewTotal();
        int blogCommentTotal = blogService.getBlogCommentTotal();
        int blogMessageTotal = blogService.getBlogMessageTotal();

        model.addAttribute("blogTotal",blogTotal);
        model.addAttribute("blogViewTotal",blogViewTotal);
        model.addAttribute("blogCommentTotal",blogCommentTotal);
        model.addAttribute("blogMessageTotal",blogMessageTotal);
        return "index :: blogMessage";
    }

}
